package main

import . "curses"
import "os"
import "fmt"
import "strings"

var text =
`def inc(x) = x + 1
##
println( "inc(16) = ", inc(16), Char::nl )

`

var acs_items = []int{
ACS_ULCORNER ,
ACS_LLCORNER ,
ACS_URCORNER ,
ACS_LRCORNER ,
ACS_LTEE  ,
ACS_RTEE  ,
ACS_BTEE  ,
ACS_TTEE  ,
ACS_HLINE ,
ACS_VLINE ,
ACS_PLUS  ,
ACS_S1  ,
ACS_S9  ,
ACS_DIAMOND ,
ACS_CKBOARD ,
ACS_DEGREE ,
ACS_PLMINUS ,
ACS_BULLET ,
ACS_LARROW ,
ACS_RARROW ,
ACS_DARROW ,
ACS_UARROW ,
ACS_BOARD ,
ACS_LANTERN ,
ACS_BLOCK ,
ACS_S3  ,
ACS_S7  ,
ACS_LEQUAL ,
ACS_GEQUAL ,
ACS_PI  ,
ACS_NEQUAL ,
ACS_STERLING ,
}

func main() {
	x := 10;
	y := 10;

	startGoCurses();
	defer stopGoCurses();
	
	Init_pair(1, COLOR_RED, COLOR_BLACK);
    Init_pair(2, COLOR_GREEN, 7)
	
	loop(x, y);
}

func startGoCurses() {
	Initscr();
	if Stdwin == nil {
		stopGoCurses();
		os.Exit(1);
	}
	
	Noecho();
	
	// Curs_set(CURS_HIDE);
	
	Stdwin.Keypad(true);
    EnableMouse();
	
	if err := Start_color(); err != nil {
		fmt.Printf("%s\n", err.String());
		stopGoCurses();
		os.Exit(1);
	}
    InitColor( 7, 500, 500, 0 )
}

func stopGoCurses() {
	Endwin();
}

func loop(x, y int) {
    x, y = 0, 0
    Stdwin.Clear()
    lines := strings.Split( text, "\n", -1 )
    for _, line := range lines {
        y += 1
        x = 1
        attributes := int(Color_pair(2)) 
        if y & 1 == 0 { Stdwin.SetAttributes( attributes ) } else { Stdwin.RemoveAttributes( attributes ) } 
        for _, ch := range line {
            Stdwin.Addch( x, y, int32(ch), Color_pair(0) )
            x += 1
        }
    }
    x, y = 0, 0
    Stdwin.Move( x, y )

    // framey things

    // loopy things

    for {
        displayFrame()
	inp := Stdwin.Getch();
	if inp == 'q' { break; }
    // if inp == '!' { Init_pair(2, COLOR_RED, COLOR_BLUE); Stdwin.Addch( 0, 20, int32(MY_ULC), Color_pair(0) ) }
	if inp == KEY_LEFT { x = x - 1; }
    if inp == KEY_MOUSE { // 0631 /* KEY_MOUSE */ {
        mx, my, mbuttons := MouseEvent()
        _ = mbuttons
        Stdwin.SetAttributes( int(Color_pair(1)) )
        Stdwin.Addstr( 10, 1, "mx = %v, my = %v", 0, mx, my )
        Stdwin.RemoveAttributes( int(Color_pair(1)) )
        x, y = mx, my
    }
	if inp == KEY_RIGHT { x = x + 1; }
	if inp == KEY_UP { y = y - 1; }
	if inp == KEY_DOWN { y = y + 1; }
    //    Stdwin.Addstr( 1, 1, "x = %v, y = %v", 0, x, y )
    ch := '?' // lines[y][x]
    i, j := x - 1, y - 1
    if 0 <= j && j < len(lines) {
        if 0 <= i && i < len(lines[j]) {
            ch = int(lines[j][i])
        }
    }
    Stdwin.Addch( 1, 1, int32(ch), Color_pair(0) )
    i = 10
    // for _, ch := range acs_items { Stdwin.Addch( i, 0, int32(ch), Color_pair(0) ); i += 1 }
    // Stdwin.Addch( 10, 0, ACS_LTEE, Color_pair(0) )
    // for i := 11; i < 40; i += 1 { Stdwin.Addch( i, 0, ACS_HLINE, Color_pair(0) ) }
    // Stdwin.Addch( 40, 0, ACS_RTEE, Color_pair(0) )

    // _ = ch; Stdwin.Addch( 0, 0, int32(ACS_PI), Color_pair(0) )
	Stdwin.Move( x, y ) // Addch(x, y, '@', Color_pair(1));
	Stdwin.Refresh()
	}
}

func displayFrame() {
    cols := *Cols;
    rows := *Rows;

    for i := 0; i < cols; i += 1 { Stdwin.Addch( i, 0, ACS_HLINE, Color_pair(0) ) }
    for i := 0; i < cols; i += 1 { Stdwin.Addch( i, rows - 1, ACS_HLINE, Color_pair(0) ) }
    for j := 0; j < rows; j += 1 { Stdwin.Addch( 0, j, ACS_VLINE, Color_pair(0) ) }    
    for j := 0; j < rows; j += 1 { Stdwin.Addch( cols - 1, j, ACS_VLINE, Color_pair(0) ) }
     Stdwin.Addch( 0, 0, ACS_ULCORNER, Color_pair(0) )
     Stdwin.Addch( cols - 1, 0, ACS_URCORNER, Color_pair(0) )
     Stdwin.Addch( 0, rows - 1, ACS_LLCORNER, Color_pair(0) )
     Stdwin.Addch( cols - 1, rows - 1, ACS_LRCORNER, Color_pair(0) )

}

